package com.peas.platform.module.icims.base.auth.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.PermissionRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.po.PermissionPo;
import com.peas.platform.module.icims.base.auth.domain.PermissionDomain;
import com.peas.platform.module.icims.base.auth.dto.PermissionDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class PermissionDomainImpl extends BaseDomainImpl<PermissionDto, PermissionPo> implements PermissionDomain {

    @Resource
    private PermissionRepository permissionRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<PermissionPo> getMapper() {
        return permissionRepository;
    }

    @Override
    public Class<? extends BaseRepository<PermissionPo>> getRepositoryClass() {
        return PermissionRepository.class;
    }

    @Transactional
    @Override
    public int save(PermissionDto dto) {
        if (StrUtil.isBlank(dto.getPermissionCode())) {
            String code = sequenceService.genSequence("base", "PermissionDto", "PD", 4, 1);
            dto.setPermissionCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("permissionCode", dto.getPermissionCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(PermissionDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("permissionCode", dto.getPermissionCode());
        return result;
    }

}
